// const hst = 'http://192.168.0.2:813'
const hst = 'https://huke.j.xueao400.com'

module.exports = {
	request(props = {}) {
		return new Promise((resolve, reject) => {
			var obj = {}
			if (props.wrToken != false) {
				const value = uni.getStorageSync('hk_token');
				if (value != '' && value != null && value != 'undefined') {
					props.header ? props.header.token = value : obj.token = value
				} else {
					uni.reLaunch({
						url: '/pages/login/login'
					})
					return
				}
			}

			if (props.unishow == undefined) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
			}
			uni.request({
				url: hst + props.url,
				data: props.data || {},
				header: props.header || {
					...obj
				},
				method: props.method || 'POST',
				success: res => {
					uni.hideLoading();
					if (res.data.code == 401) {
						uni.removeStorageSync('token_tiem');
						uni.removeStorageSync('hk_token');
						uni.showModal({
							title: '提示',
							content: '登录过期或未登录',
							showCancel: false,
							success: (res) => {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}
						});
						props.fail && props.fail(res.data)
						return
					};
					if (res.data.code == 500) {
						if (!props.fail) {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								showCancel: false,
								success: (res) => {

								}
							});
						}
						props.fail && props.fail(res.data)
						return
					};
					if (res.data.code != 200) {
						if (!props.fail) {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								showCancel: false,
								success: (res) => {

								}
							});
						}

						props.fail && props.fail(res.data)
						return
					}
					resolve(res.data)
					props.success && props.success(res.data)

				},
				fail: error => {
					uni.hideLoading();
					uni.showToast({
						title: '网络连接超时，请检查网络',
						icon: 'none'
					})
					props.fail && props.fail(error)
					reject(error)
				},
				complete: res => {
					// console.log(res)
					if (res.statusCode == 500) {
						uni.showToast({
							title: '服务器驾崩了，',
							icon: 'none'
						});
					} else if (res.statusCode == 404) {
						uni.showToast({
							title: '你访问的资源不存在',
							icon: 'none'
						});
					}
					props.complete && props.complete(res)

				}
			})
		})
	},
	updateFile(data) {
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '加载中...'
			})
			console.log('文件上传', data);
			try {
				uni.uploadFile({
					url: hst + '/api/other.file/upload',
					filePath: data.url,
					name: 'file',
					success: (res) => {
						console.log(res)
						res = JSON.parse(res.data)

						if (res.code != 200) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
							console.log(1)
							data.fail && data.fail(res)
							data.complete && data.complete(res)
							reject()
							return
						}
						data.success && data.success(res)
						resolve(res)
					},
					fail: err => {
						console.log(2)
						data.fail && data.fail(err)
						data.complete && data.complete(err)
						reject()
					},
					complete: res => {
						console.log(3);
						uni.hideLoading();
						if (res.statusCode == 500) {
							uni.showToast({
								title: '服务器驾崩了',
								icon: 'none'
							});
						} else if (res.statusCode == 404) {
							uni.showToast({
								title: '你访问的资源不存在',
								icon: 'none'
							});
						}
						data.complete && data.complete(res)
					}
				})
			} catch (e) {
				console.log('捕获错误', e);
			}
		})
	},



}
